package com.zhaoqi.hr.controller;

import com.zhaoqi.hr.dto.request.ContractCreateRequest;
import com.zhaoqi.hr.dto.request.ContractSignRequest;
import com.zhaoqi.hr.dto.response.ApiResponse;
import com.zhaoqi.hr.dto.response.ContractResponse;
import com.zhaoqi.hr.entity.WorkContract;
import com.zhaoqi.hr.security.CurrentUser;
import com.zhaoqi.hr.security.UserPrincipal;
import com.zhaoqi.hr.service.ContractService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 电子合同控制器
 */
@RestController
@RequestMapping("/contracts")
public class ContractController {

    private static final Logger log = LoggerFactory.getLogger(ContractController.class);

    private final ContractService contractService;

    public ContractController(ContractService contractService) {
        this.contractService = contractService;
    }

    /**
     * 创建合同（雇主专用）
     */
    @PostMapping("/create")
    public ApiResponse<ContractResponse> createContract(
            @CurrentUser UserPrincipal currentUser,
            @Valid @RequestBody ContractCreateRequest request) {
        
        ContractResponse response = contractService.createContract(currentUser.getId(), request);
        return ApiResponse.success(response);
    }

    /**
     * 签署合同
     */
    @PostMapping("/sign")
    public ApiResponse<ContractResponse> signContract(
            @CurrentUser UserPrincipal currentUser,
            @Valid @RequestBody ContractSignRequest request) {
        
        ContractResponse response = contractService.signContract(currentUser.getId(), request);
        return ApiResponse.success(response);
    }

    /**
     * 获取合同详情
     */
    @GetMapping("/{contractId}")
    public ApiResponse<ContractResponse> getContract(@PathVariable Long contractId) {
        ContractResponse contract = contractService.getContractById(contractId);
        return ApiResponse.success(contract);
    }

    /**
     * 获取我的合同列表（雇主）
     */
    @GetMapping("/employer/my")
    public ApiResponse<List<ContractResponse>> getMyEmployerContracts(
            @CurrentUser UserPrincipal currentUser) {
        
        List<ContractResponse> contracts = contractService.getEmployerContracts(currentUser.getId());
        return ApiResponse.success(contracts);
    }

    /**
     * 获取我的合同列表（劳动者）
     */
    @GetMapping("/worker/my")
    public ApiResponse<List<ContractResponse>> getMyWorkerContracts(
            @CurrentUser UserPrincipal currentUser) {
        
        List<ContractResponse> contracts = contractService.getWorkerContracts(currentUser.getId());
        return ApiResponse.success(contracts);
    }

    /**
     * 更新合同状态（管理员专用）
     */
    @PutMapping("/{contractId}/status")
    public ApiResponse<ContractResponse> updateContractStatus(
            @PathVariable Long contractId,
            @RequestParam WorkContract.ContractStatus status) {
        
        ContractResponse response = contractService.updateContractStatus(contractId, status);
        return ApiResponse.success(response);
    }
}
